package com.boxin.ims.modules.wechat.busi.metal;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.html.utils.HtmlUtils;
import com.boxin.ims.modules.wechat.busi.BusinessProcessor;
import com.boxin.ims.modules.wechat.busi.ProcessMessage;
import com.boxin.ims.modules.wechat.entity.TextMessage;
import com.thinkgem.jeesite.common.utils.StringUtils;

/**
 * @author Administrator
 * @date 2014-3-19
 * @function <p>国际现货白银 </p>
 */
@Service
public class SilverPriceProcessor extends BusinessProcessor{

	
	
	@Override
	public ProcessMessage process() {

		String keyword = textMessage.getContent();
		
		String title = "";
		String url = "http://jiage.me/PriceList/price.php?type=guojiyin";	//国际现货白银		by
		if("by".equalsIgnoreCase(keyword)){						//国际现货白银
			title="【现货白银】";
			url = "http://jiage.me/PriceList/price.php?type=guojiyin";
		}else if("hj".equalsIgnoreCase(keyword)){			////国际现货黄金
			title = "【现货黄金】";
			url = "http://jiage.me/PriceList/price.php?type=guojijin";	
		}else if("zby".equalsIgnoreCase(keyword)){
			title ="【工商银行纸白银】";
			url = "http://jiage.me/PriceList/price.php?type=icbcyin";		//	纸白银
			
		}else if("zhj".equalsIgnoreCase(keyword)){
			title ="【工商银行纸黄金】";
			url = "http://jiage.me/PriceList/price.php?type=icbccny";		//纸黄金
			
		}else if("zbai".equalsIgnoreCase(keyword)){							//纸铂金
			title ="【工商银行纸铂金】";
			url = "http://jiage.me/PriceList/price.php?type=icbcbai";		//纸黄金
			
		}else if("mz".equalsIgnoreCase(keyword)){
			title="【美元指数】";
			url = "http://jiage.me/PriceList/price.php?type=meizhi";		//美元指数			mz
		}else if("oy".equalsIgnoreCase(keyword)){							//欧元指数
			title="【欧元指数】";
		}
		
		
		
			   
		
		try {
			Document  doc =  HtmlUtils.getHtmlDocument(url);
			Elements elements = doc.select("div p");
			StringBuffer buffer = new StringBuffer();
			buffer.append(title).append("\n");
			for(Element elt : elements){
				String text = elt.text()  ;
				if(text.contains("jiage")){
					continue;
				}
				
//				if(text.indexOf("卖出")>0){
//					StringBuffer temp = new StringBuffer(text);
//					int n = text.indexOf("卖出");
//					temp.insert(n, "\n");
//				}
				
				
				if(StringUtils.isNotEmpty(text)){
					buffer.append(text).append("\n");
				}
			}
			
			
			//System.out.println(buffer.toString());
			String content = buffer.toString();
			content = content.replaceAll("卖出价", "\n卖出价");
			content = content.replaceAll("最低价", "\n最低价");
			content = content.replaceAll("涨跌", "\n涨跌");
			writeToUser(content);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	
	
	@Override
	public boolean isNeedBeMember() {
		
		return false;
	}
	
	
	public static void main(String[] args) {
		SilverPriceProcessor processor = new SilverPriceProcessor();
		TextMessage txtMessage = new TextMessage();
		txtMessage.setContent("zhj");
		processor.setTextMessage(txtMessage);
		processor.process();
		
	}
	
		

}
